<?php

namespace app\home\controller;

use app\company\service\DataService;
use library\Controller;
use think\Db;

/**
 * 计价规则管理
 * Class User
 * @package app\worker\controller
 */
class PriceRules extends Controller
{

    /**
     * 绑定当前数据表
     * @var string
     */
    protected $table = 'PriceRules';

    /**
     * 计价规则管理
     * @auth true
     * @menu true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function index()
    {
        $this->title = '计价规则';
        $query = $this->_query($this->table)
        ->where(['is_deleted' => '0']);
        if(request()->get('platform_id')){
            $query->where('platform_id', request()->get('platform_id'));
        }
        $query->order('city_level asc,jobs asc')->page();
    }

    /**
     * 添加计价规则
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function add()
    {
        $this->_form($this->table, 'form');
    }

    /**
     * 修改计价规则
     * @auth true
     * @throws \think\Exception
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     * @throws \think\exception\PDOException
     */
    public function edit()
    {
        $this->_form($this->table, 'form');
    }

    /**
     * 权限表单数据处理
     * @param array $data 表单数据
     * @throws \think\db\exception\DataNotFoundException
     * @throws \think\db\exception\ModelNotFoundException
     * @throws \think\exception\DbException
     */
    protected function _form_filter(&$data)
    {
        if(request()->isPost()){
            if(!isset($data['id'])){
                $where = [
                    'city_level' => $data['city_level'],
                    'type' => $data['type'],
                    'jobs' => $data['jobs'],
                    'is_deleted' => 0,
                    'platform_id' => $data['platform_id'],
                ];
                if(Db::name($this->table)->where($where)->find()){
                    $this->error('该计价规则已存在');
                }
            }
        }
    }

    /**
     * 删除计价规则
     * @auth true
     * @throws \think\Exception
     * @throws \think\exception\PDOException
     */
    public function remove()
    {
        $this->_delete($this->table);
    }

}
